
package com.bluelinelabs.conductor.demo.controllers;

import ohos.aafwk.content.IntentParams;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import com.bluelinelabs.conductor.autodispose.ControllerScopeProvider;
import com.bluelinelabs.conductor.conductor.Controller;
import com.bluelinelabs.conductor.conductor.RouterTransaction;
import com.bluelinelabs.conductor.conductor.changehandler.HorizontalChangeHandler;
import com.bluelinelabs.conductor.demo.ResourceTable;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;

import static com.uber.autodispose.AutoDispose.autoDisposable;

/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class AutodisposeController extends Controller {
    private String TAG = "AutodisposeController";

    public static final String KEY_BackgroundColor = "AutodisposeController.BackgroundColor";

    private ControllerScopeProvider scopeProvider = ControllerScopeProvider.from(this);

    public AutodisposeController() {
    }

    protected AutodisposeController(IntentParams args) {
        super(args);
    }

    {
        Disposable disposing_from_constructor = Observable.interval(1, TimeUnit.SECONDS)
            .doOnDispose(new Action() {
                @Override
                public void run() throws Exception {
                    autoDisposable(scopeProvider);
                }
            })
            .subscribe();
        if (disposing_from_constructor.isDisposed()) {
        }
    }


    @Override
    protected ohos.agp.components.Component onCreateView(LayoutScatter inflater, ComponentContainer container, IntentParams savedViewState) {


        ohos.agp.components.Component parse = inflater.getInstance(container.getContext()).parse(ResourceTable.Layout_controller_lifecycle, container, false);
        Text Id_title = (Text) parse.findComponentById(ResourceTable.Id_title);
        Id_title.setText(getActivity().getString(ResourceTable.String_rxlifecycle_title, TAG));

        Button Id_next_retain_view = (Button) parse.findComponentById(ResourceTable.Id_next_retain_view);
        Id_next_retain_view.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(ohos.agp.components.Component component) {
                IntentParams intentParams = getArgs();
                if (intentParams == null) {
                    intentParams = new IntentParams();
                }
                intentParams.setParam(TextController.KEY_TEXT, "Logcat should now report that the observables from onAttach() has been disposed of, while the constructor and onViewBound() observables are still running.");
                getRouter().pushController(
                    RouterTransaction.with(new TextController(intentParams, 1))
                        .pushChangeHandler(new HorizontalChangeHandler())
                        .popChangeHandler(new HorizontalChangeHandler())
                );
            }
        });
        Button Id_next_release_view = (Button) parse.findComponentById(ResourceTable.Id_next_release_view);
        Id_next_release_view.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(ohos.agp.components.Component component) {
                IntentParams intentParams = getArgs();
                if (intentParams == null) {
                    intentParams = new IntentParams();
                }
                intentParams.setParam(TextController.KEY_TEXT, "Logcat should now report that the observables from onAttach() and onViewBound() have been disposed of, while the constructor observable is still running.");
                getRouter().pushController(
                    RouterTransaction.with(new TextController(intentParams, 2))
                        .pushChangeHandler(new HorizontalChangeHandler())
                        .popChangeHandler(new HorizontalChangeHandler())
                );
            }
        });
        if (getArgs() != null && getArgs().getParam(KEY_BackgroundColor) != null) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor((String) getArgs().getParam(KEY_BackgroundColor))));
            parse.setBackground(shapeElement);
        }


        return parse;

    }


    @Override
    public void onAttach(Component view) {
        super.onAttach(view);


    }

    @Override
    public void onDestroyView(Component view) {
        super.onDestroyView(view);

    }

    public void changeTAG(String tag) {
        TAG = tag;
    }

}
